<?php

namespace app\shop\controller;

use think\Controller;

class Cart extends Controller
{
    protected $busid;
    protected $Business;

    public function _initialize()
    {
        // 继承父类
        parent::_initialize();
        $this->BusinessModel = model('Business.Business');
        $this->ProductModel = model('Product.Product');
        $this->PropModel = model('Product.Prop');
        $this->RelationModel = model('Product.Relation');
        $this->CartModel = model('Product.Cart');
        $this->busid = $this->request->param('busid', 0, 'trim');

        $this->Business = $this->BusinessModel->find($this->busid);

        if (!$this->Business)
        {
            $this->error('用户不存在');
            exit;
        }
    }

    // 添加
    public function  add()
    {
        if ($this->request->isPost())
        {
            $proid = $this->request->param('proid', 0, 'trim');
            $nums = $this->request->param('nums', 0, 'trim');
            $attrs = $this->request->param('attrs', '', 'trim');
            $isDirectlyBuy = $this->request->param('isDirectlyBuy', 0, 'trim');

            $product =  $this->ProductModel->find($proid);

            if (!$proid)
            {
                $this->error('产品不存在');
                exit;
            }

            $where = [
                'busid' => $this->busid,
                'proid' => $proid,
            ];

            $data = [
                'busid' => $this->busid,
                'proid' => $proid,
                'nums' => $nums,
            ];


            // 查询商品属性信息  
            $Relationlist = $this->RelationModel->with('prop')->where('relation.id', 'IN', $attrs)->select();

            // 商品属性
            if ($Relationlist)
            {
                $attrslist = [];

                $price = $product['price'];

                foreach ($Relationlist as $item)
                {
                    $attrslist[] = [
                        'title' => $item['prop']['title'], // 商品属性名
                        'value' => $item['value'], // 属性名称
                        'price' => $item['price'] // 价格
                    ];

                    $price = bcadd($price, $item['price'], 2);
                }

                $where['attrs'] = json_encode($attrslist);

                $data['price'] = $price;
                $data['total'] = bcmul($price, $nums, 2);
                $data['attrs'] = json_encode($attrslist);
            }
            else
            {
                $data['price'] = $product['price'];
                $data['total'] = bcmul($product['price'], $nums, 2);
                $data['attrs'] = null;
            }

            // 查询购物车是否已添加
            $cartinfo = $this->CartModel->where($where)->find();

            // 已加入购物车
            if ($cartinfo && $isDirectlyBuy == 0)
            {
                $nums = bcadd($nums, $cartinfo['nums']);
                $stock = bcsub($product['stock'], $nums);
                if ($stock < 0)
                {
                    $this->error("{$product['name']}库存不足");
                    exit;
                }
                $total = bcmul($cartinfo['price'], $nums, 2);
                $data = [
                    'id' => $cartinfo['id'],
                    'nums' => $nums,
                    'total' => $total
                ];
                $result = $this->CartModel->isUpdate(true)->save($data);
                if ($result)
                {
                    $this->success('添加成功');
                }
                else
                {
                    $this->error($this->CartModel->getError());
                }
            }
            // 未加入购物车
            else
            {
                $stock = bcsub($product['stock'], $nums);

                if ($stock < 0)
                {
                    $this->error("{$product['name']}库存不足");
                    exit;
                }

                $result = $this->CartModel->save($data);
                $addId = $this->CartModel->getLastInsID();

                if ($result)
                {
                    $this->success('添加成功', null, ['cartid' => $addId]);
                }
                else
                {
                    $this->error($this->CartModel->getError());
                }
            }
        }
    }

    // 列表
    public function index()
    {
        if ($this->request->isPost())
        {
            $cartids = $this->request->param('cartids', 0, 'trim');

            $where = ['busid' => $this->busid,];

            if($cartids > 0)
            {
                $where['cart.id'] = ['IN', $cartids];
            }

            $data = $this->CartModel->with('product')->where($where)->order('id desc')->select();

            $this->success('', '', $data);
        }
    }

    // 编辑
    public function edit()
    {
        if ($this->request->isPost())
        {
            $cartid = $this->request->param('cartid', 0, 'trim');
            $nums = $this->request->param('nums', 0, 'trim');

            $cartinfo = $this->CartModel->find($cartid);

            if (!$cartinfo)
            {
                $this->error('购物车商品不存在');
                exit;
            }

            if ($nums <= 0)
            {
                $this->error('商品数量不能为小于0');
                exit;
            }

            $total = bcmul($cartinfo['price'], $nums, 2);
            $data = [
                'id' => $cartinfo['id'],
                'nums' => $nums,
                'total' => $total
            ];
            $result = $this->CartModel->isUpdate(true)->save($data);
            if ($result)
            {
                $this->success('操作成功');
            }
            else
            {
                $this->error($this->CartModel->getError());
            }
        }
    }

    // 删除
    public function del()
    {
        if ($this->request->isPost())
        {
            $cartid = $this->request->param('cartid', 0, 'trim');

            $cartinfo = $this->CartModel->find($cartid);

            if (!$cartinfo)
            {
                $this->error('购物车商品不存在');
                exit;
            }

            $result = $this->CartModel->destroy($cartid);
            if ($result)
            {
                $this->success('操作成功');
            }
            else
            {
                $this->error($this->CartModel->getError());
            }
        }
    }
}
